package MAgentAssignment.gui;

import MAgentAssignment.ontology.concepts.Airport;
import MAgentAssignment.ontology.concepts.Flight;
import MAgentAssignment.ontology.concepts.Location;

import javax.swing.*;
import java.awt.*;
import java.util.Locale;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: Prageeth
 * Date: 2/16/12
 * Time: 8:17 PM
 * To change this template use File | Settings | File Templates.
 */
public class FlightWorldTextArea extends JTextArea
{
    private boolean changing = false;
    private Vector<FlightIcon> flightIcons = new Vector<FlightIcon>();
    private Vector<AirportIcon> airportIcons = new Vector<AirportIcon>();


    public void init()
    {

        AirportIcon airportIcon = new AirportIcon();
        Airport newDelhi = new Airport();
        newDelhi.setAirportName( "New Delhi" );
        newDelhi.setAirpotCode( "DEL" );
        newDelhi.setLocation( new Location( 10, 10 ) );
        airportIcon.setAirport( newDelhi );
        airportIcons.add( airportIcon );

        airportIcon = new AirportIcon();
        Airport checnnai = new Airport();
        checnnai.setAirportName( "Chennai" );
        checnnai.setAirpotCode( "CEN" );
        checnnai.setLocation( new Location( 10, 200 ) );
        airportIcon.setAirport( checnnai );
        airportIcons.add( airportIcon );

        airportIcon = new AirportIcon();
        Airport colombo = new Airport();
        colombo.setAirportName( "Colombo" );
        colombo.setAirpotCode( "CMB" );
        colombo.setLocation( new Location( 400, 10 ) );
        airportIcon.setAirport( colombo );
        airportIcons.add( airportIcon );

        airportIcon = new AirportIcon();
        Airport hambanthota = new Airport();
        hambanthota.setAirportName( "Hambanthota" );
        hambanthota.setAirpotCode( "HMB" );
        hambanthota.setLocation( new Location( 123, 300 ) );
        airportIcon.setAirport( hambanthota );
        airportIcons.add( airportIcon );

        airportIcon = new AirportIcon();
        Airport male = new Airport();
        male.setAirportName( "Male" );
        male.setAirpotCode( "MAL" );
        male.setLocation( new Location( 100, 350 ) );
        airportIcon.setAirport( male );
        airportIcons.add( airportIcon );

        airportIcon = new AirportIcon();
        Airport gatwick = new Airport();
        gatwick.setAirportName( "Gatwick" );
        gatwick.setAirpotCode( "GWT" );
        gatwick.setLocation( new Location( 300, 300 ) );
        airportIcon.setAirport( gatwick );
        airportIcons.add( airportIcon );

        flightIcons = new Vector<FlightIcon>();
        FlightIcon flightIcon = new FlightIcon();
        flightIcon.init();
        flightIcon.setLocationTo( (Location) colombo.getLocation().clone() );
        flightIcon.setLocationFrom( (Location) hambanthota.getLocation().clone() );
        flightIcons.add( flightIcon );

        flightIcon = new FlightIcon();
        flightIcon.init();
        flightIcon.setLocationTo( (Location) male.getLocation().clone() );
        flightIcon.setLocationFrom( (Location) gatwick.getLocation().clone() );
        flightIcons.add( flightIcon );

        flightIcon = new FlightIcon();
        flightIcon.init();
        flightIcon.setLocationTo( (Location) gatwick.getLocation().clone() );
        flightIcon.setLocationFrom( (Location) colombo.getLocation().clone() );
        flightIcons.add( flightIcon );

        flightIcon = new FlightIcon();
        flightIcon.init();
        flightIcon.setLocationTo( (Location) colombo.getLocation().clone() );
        flightIcon.setLocationFrom( (Location) newDelhi.getLocation().clone() );
        flightIcons.add( flightIcon );

        flightIcon = new FlightIcon();
        flightIcon.init();
        flightIcon.setLocationTo( (Location) newDelhi.getLocation().clone() );
        flightIcon.setLocationFrom( (Location) gatwick.getLocation().clone() );
        flightIcons.add( flightIcon );
    }

    @Override public void paint( Graphics g )
    {
        super.paint( g );
        {
            if( !changing )
            {
                for( int i = 0; i < flightIcons.size(); i++ )
                {
                    FlightIcon flightIcon = (FlightIcon) flightIcons.get( i );
                    flightIcon.paint( g );
                }
            }
            if( !changing )
            {
                for( int i = 0; i < airportIcons.size(); i++ )
                {
                    AirportIcon airportIcon = airportIcons.get( i );
                    airportIcon.paint( g );
                }
            }
        }
    }

    public Vector<FlightIcon> getFlightIcons()
    {
        return flightIcons;
    }

    public void setFlightIcons( Vector<FlightIcon> flightIcons )
    {
        this.flightIcons = flightIcons;
    }

    public void tickStep()
    {
        for( int i = 0; i < flightIcons.size(); i++ )
        {
            FlightIcon flightIcon = (FlightIcon) flightIcons.get( i );
            flightIcon.moveStep();
        }
    }

    public void addAirport( Airport airport )
    {
        changing = true;
        for( int i = 0; i < airportIcons.size(); i++ )
        {
            AirportIcon temp = airportIcons.get( i );
            if( temp.getAirport().getAirpotCode().equals( airport.getAirpotCode() ) )
            {
                temp.setAirport( airport );
                return;
            }
        }
        final AirportIcon airportIcon = new AirportIcon();
        airportIcon.setAirport( airport );
        airportIcon.addAirportStatusListener( new AirportStatusListener()
        {
            public void closed()
            {
                airportIcons.remove( airportIcon );
            }
        } );
        airportIcons.add( airportIcon );
        changing = false;
    }

    public void addFlight( Flight flight, FlightTravelListener flightTravelListener )
    {
        changing = true;
        final FlightIcon flightIcon = new FlightIcon();
        flightIcon.setFlight( flight );
        for( int i = 0; i < flightIcons.size(); i++ )
        {
            FlightIcon temp = (FlightIcon) flightIcons.get( i );
            if( temp.getFlight().getFlightNo().equals( flight.getFlightNo() ) )
            {
                return;
            }
        }

        flightIcon.addTravelListener( flightTravelListener );
        flightIcon.addTravelListener( new FlightTravelListener()
        {
            public void arrived( FlightIcon removingIcon )
            {
                //                removingIcon.cancelFlight();
                flightIcons.remove( removingIcon );
            }
        } );
        flightIcon.setLocationFrom( (Location) flightIcon.getFlight().getFromAirport().getLocation().clone() );
        flightIcon.setLocationTo( (Location) flightIcon.getFlight().getToAirport().getLocation().clone() );
        flightIcons.add( flightIcon );
        changing = false;
    }

    public void removeFlight( Flight flight )
    {
        changing = true;
        Vector<FlightIcon> removingIcons = new Vector<FlightIcon>();

        for( FlightIcon flightIcon : flightIcons )
        {
            if( flightIcon.getFlight().getFlightNo().equals( flight.getFlightNo() ) )
            {
                flightIcon.cancelFlight();
                removingIcons.add( flightIcon );
            }
        }
        flightIcons.removeAll( removingIcons );
        changing = false;
    }

    public void removeAirports( Airport airport )
    {
        changing = true;
        Vector<AirportIcon> removingIcons = new Vector<AirportIcon>();

        for( AirportIcon airportIcon : airportIcons )
        {
            if( airportIcon.getAirport().getAirpotCode().equals( airport.getAirpotCode() ) )
            {
                airportIcon.cancelAirport();
                removingIcons.add( airportIcon );
            }
        }
        airportIcons.removeAll( airportIcons );
        changing = false;

    }
}
